<?php namespace Modules\Web\Http\Controllers;

use Pingpong\Modules\Routing\Controller;

class BrandController extends Controller {
	
	public function index()
	{
        /**
         * ECSHOP 品牌列表
         * ============================================================================
         * * 版权所有 2005-2012 上海商派网络科技有限公司，并保留所有权利。
         * 网站地址: http://www.ecshop.com；
         * ----------------------------------------------------------------------------
         * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
         * 使用；不允许对程序代码以任何形式任何目的的再发布。
         * ============================================================================
         * $Author: liubo $
         * $Id: brand.php 17217 2011-01-19 06:29:08Z liubo $
         */

        define('IN_ECS', true);

        require(dirname(__FILE__) . '/includes/init.php');

        if ((DEBUG_MODE & 2) != 2)
        {
            $smarty->caching = true;
        }

        /*------------------------------------------------------ */
//-- INPUT
        /*------------------------------------------------------ */

        /* 获得请求的分类 ID */
        if (!empty($_REQUEST['id']))
        {
            $brand_id = intval($_REQUEST['id']);
        }
        if (!empty($_REQUEST['brand']))
        {
            $brand_id = intval($_REQUEST['brand']);
        }
        if (empty($brand_id))
        {
            /* 缓存编号 */
            $cache_id = sprintf('%X', crc32($_CFG['lang']));
            if (!$smarty->is_cached('brand_list.dwt', $cache_id))
            {
                assign_template();
                $position = assign_ur_here('', $_LANG['all_brand']);
                $smarty->assign('page_title',      $position['title']);    // 页面标题
                $smarty->assign('ur_here',         $position['ur_here']);  // 当前位置

                $smarty->assign('categories',      get_categories_tree()); // 分类树
                $smarty->assign('helps',           get_shop_help());       // 网店帮助
                $smarty->assign('top_goods',       get_top10());           // 销售排行

                $smarty->assign('brand_list', get_brands());
            }
            $smarty->display('brand_list.dwt', $cache_id);
            exit();
        }

        /* 初始化分页信息 */
        $page = !empty($_REQUEST['page'])  && intval($_REQUEST['page'])  > 0 ? intval($_REQUEST['page'])  : 1;
        $size = !empty($_CFG['page_size']) && intval($_CFG['page_size']) > 0 ? intval($_CFG['page_size']) : 10;
        $cate = !empty($_REQUEST['cat'])   && intval($_REQUEST['cat'])   > 0 ? intval($_REQUEST['cat'])   : 0;

        /* 排序、显示方式以及类型 */
        $default_display_type = $_CFG['show_order_type'] == '0' ? 'list' : ($_CFG['show_order_type'] == '1' ? 'grid' : 'text');
        $default_sort_order_method = $_CFG['sort_order_method'] == '0' ? 'DESC' : 'ASC';
        $default_sort_order_type   = $_CFG['sort_order_type'] == '0' ? 'goods_id' : ($_CFG['sort_order_type'] == '1' ? 'shop_price' : 'last_update');

        $sort  = (isset($_REQUEST['sort'])  && in_array(trim(strtolower($_REQUEST['sort'])), array('goods_id', 'shop_price', 'last_update'))) ? trim($_REQUEST['sort'])  : $default_sort_order_type;
        $order = (isset($_REQUEST['order']) && in_array(trim(strtoupper($_REQUEST['order'])), array('ASC', 'DESC')))                              ? trim($_REQUEST['order']) : $default_sort_order_method;
        $display  = (isset($_REQUEST['display']) && in_array(trim(strtolower($_REQUEST['display'])), array('list', 'grid', 'text'))) ? trim($_REQUEST['display'])  : (isset($_COOKIE['ECS']['display']) ? $_COOKIE['ECS']['display'] : $default_display_type);
        $display  = in_array($display, array('list', 'grid', 'text')) ? $display : 'text';
        setcookie('ECS[display]', $display, gmtime() + 86400 * 7);

        /*------------------------------------------------------ */
//-- PROCESSOR
        /*------------------------------------------------------ */

        /* 页面的缓存ID */
        $cache_id = sprintf('%X', crc32($brand_id . '-' . $display . '-' . $sort . '-' . $order . '-' . $page . '-' . $size . '-' . $_SESSION['user_rank'] . '-' . $_CFG['lang'] . '-' . $cate));

        if (!$smarty->is_cached('brand.dwt', $cache_id))
        {
            $brand_info = get_brand_info($brand_id);

            if (empty($brand_info))
            {
                ecs_header("Location: ./\n");
                exit;
            }

            $smarty->assign('data_dir',    DATA_DIR);
            $smarty->assign('keywords',    htmlspecialchars($brand_info['brand_desc']));
            $smarty->assign('description', htmlspecialchars($brand_info['brand_desc']));

            /* 赋值固定内容 */
            assign_template();
            $position = assign_ur_here($cate, $brand_info['brand_name']);
            $smarty->assign('page_title',     $position['title']);   // 页面标题
            $smarty->assign('ur_here',        $position['ur_here']); // 当前位置
            $smarty->assign('brand_id',       $brand_id);
            $smarty->assign('category',       $cate);

            $smarty->assign('categories',     get_categories_tree());        // 分类树
            $smarty->assign('helps',          get_shop_help());              // 网店帮助
            $smarty->assign('top_goods',      get_top10());                  // 销售排行
            $smarty->assign('show_marketprice', $_CFG['show_marketprice']);
            $smarty->assign('brand_cat_list', brand_related_cat($brand_id)); // 相关分类
            $smarty->assign('feed_url',       ($_CFG['rewrite'] == 1) ? "feed-b$brand_id.xml" : 'feed.php?brand=' . $brand_id);

            /* 调查 */
            $vote = get_vote();
            if (!empty($vote))
            {
                $smarty->assign('vote_id',     $vote['id']);
                $smarty->assign('vote',        $vote['content']);
            }

            $smarty->assign('best_goods',      brand_recommend_goods('best', $brand_id, $cate));
            $smarty->assign('promotion_goods', brand_recommend_goods('promote', $brand_id, $cate));
            $smarty->assign('brand',           $brand_info);
            $smarty->assign('promotion_info', get_promotion_info());

            $count = goods_count_by_brand($brand_id, $cate);

            $goodslist = brand_get_goods($brand_id, $cate, $size, $page, $sort, $order);

            if($display == 'grid')
            {
                if(count($goodslist) % 2 != 0)
                {
                    $goodslist[] = array();
                }
            }
            $smarty->assign('goods_list',      $goodslist);
            $smarty->assign('script_name', 'brand');

            assign_pager('brand',              $cate, $count, $size, $sort, $order, $page, '', $brand_id, 0, 0, $display); // 分页
            assign_dynamic('brand'); // 动态内容
        }

        $smarty->display('brand.dwt', $cache_id);

		return view('web::index');
	}
	
}